package com.fly.lambda.service;

import com.fly.lambda.dao.MessageDAO;
import com.fly.lambda.domain.Message;
import com.fly.lambda.framework.dao.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Fly on 2016/7/29.
 */
@Service
public class MessageService {

    @Autowired
    MessageDAO messageDAO;

    /**
     * 添加留言
     * @param message
     */
    public void createMessage(Message message){
        messageDAO.create(message);
    }

    /**
     * 查询留言
     * @param read  是否已读
     * @param curPage
     * @param pageSize
     * @return
     */
    public Pagination<Message> queryMessagePage(String read, int curPage, int pageSize){
        return messageDAO.queryMessagePage(read, curPage, pageSize);
    }

    /**
     * 删除留言
     * @param messageId 留言id
     */
    public void deleteMessage(String messageId){
        messageDAO.deleteById(messageId);
    }

    /**
     * 将留言设为已读
     * @param messageId 留言id
     */
    public void readMessage(String messageId){
        Message message = messageDAO.getById(messageId);
        if (null != message){
            message.setIsRead(Message.READ_YES);
            messageDAO.update(message);
        }
    }

    public int getUnreadCount(){
        return messageDAO.unreadCount();
    }
}
